<?php

include_once("core/controller.php");
include_once("core/versionHandler.php");
include_once("models/maps/map.php");

/**
 * @copyright joel SIMOes
 * @package map
 *
 */
class mapList extends Controller{

	private $page;
	private $numPerPage;
	private $order;
	private $orderKind;
	private $parameter;
	private $value;
	private $range = 10;
	private $numMaps;

	public function isLegitimate(){
		return true;
	}

	public function actions(){
		$versionH = new VersionHandler("");
		$this->response->add($versionH->getMyVersion());
		$maps		= new Maps();
		$filter = $this->browseFiltered();
		$this->numMaps = $maps->getNumMaps($filter);


		$sql  = "";
		$sql .= $filter;
		$sql .= $this->browseOrder();
		$sql .= $this->browsePerPage();

		$nodeMaps  = $maps->getMapsComplex($sql);
		$this->response->add($nodeMaps);

		//Adds navigations informations to the XML node
		$NavigationNode = $this->response->doc->createElement("navigation");
		$this->browsePerPageLinks($NavigationNode);

		$orderNode      = $this->response->doc->createElement("order");
		$orderNode->appendChild($this->response->doc->createTextNode($this->order));
		$NavigationNode->appendChild($orderNode);

		$orderKindNode  = $this->response->doc->createElement("orderkind");
		$orderKindNode->appendChild($this->response->doc->createTextNode($this->orderKind));
		$NavigationNode->appendChild($orderKindNode);

		$numResNode  = $this->response->doc->createElement("numresults");
		$numResNode->appendChild($this->response->doc->createTextNode($this->numMaps));
		$NavigationNode->appendChild($numResNode);

		$parameterNode  = $this->response->doc->createElement("parameter");
		$parameterNode->setAttribute("param",$this->parameter);
		$parameterNode->appendChild($this->response->doc->createTextNode($this->value));
		$NavigationNode->appendChild($parameterNode);

		$this->response->add($NavigationNode);

		$dateNode = $this->response->doc->createElement("date");
		$dateNode->appendChild($this->response->doc->createTextNode(date("d/m/Y G:i")));
		$this->response->add($dateNode);


		$this->response->setXSLT("views/maps/maps.xsl");
	}

	public function defaultActions(){
		$this->response->setXSLT("views/login.xsl");
	}



	//Page Per Page Navigation Tool
	private function browsePerPage(){
		$this->numPerPage = 30;
		if( $this->request->get("page")!=null && ereg("[0-9]",$this->request->get("page"))){
			$this->page = $this->request->get("page");
			if( $this->request->get("numpage")!=null && ereg("[0-9]",$this->request->get("numpage"))){
				$this->numPerPage = $this->request->get("numpage");
			}else{
				$this->numPerPage = 25;
			}
		}else{
			$this->page       = 1;
			$this->numPerPage = 25;
		}
		$numPages = ceil($this->numMaps/$this->numPerPage);
		if($this->page > $numPages){
			$this->page = $numPages;
		}
		if($this->page<=0) $this->page=1;
		return " LIMIT ".($this->page-1)*$this->numPerPage.",".$this->numPerPage;
	}


	//Filter Navigation Tool
	private function browseFiltered(){
		$this->numPerPage = 30;
		if( $this->request->get("parameter")!=null && ereg("[%-_a-zA-Z0-9]",$this->request->get("parameter"))){
			if( $this->request->get("value")!=null && ereg("[%-_a-zA-Z0-9]",$this->request->get("value"))){
				$this->parameter = $this->request->get("parameter");
				$this->value     = $this->request->get("value");
			}else{
				return "";
			}
		}else{
			return "";
		}
		return " WHERE `".$this->parameter."` LIKE '".$this->value."' ";
	}

	//Order Navigation Tool
	private function browseOrder(){
		$this->order      = "";
		$this->orderKind  = "";
		if( $this->request->get("order")!=null && ereg("[-_a-zA-Z0-9]",$this->request->get("order"))){
			if( $this->request->get("orderkind")!=null &&  ($this->request->get("orderkind")=="asc" || $this->request->get("orderkind")=="desc" )){
				$this->orderKind = $this->request->get("orderkind");
				$this->order     = $this->request->get("order");
			}else{
				return "";
			}
		}else{
			return "";
		}

		return " ORDER BY `".$this->order."` ".$this->orderKind;
	}




	//returns elements in the navigation node
	private function browsePerPageLinks($NavigationNode){
		 
		$pageNode       = $this->response->doc->createElement("page");
		$pageNode->appendChild($this->response->doc->createTextNode($this->page));
		$NavigationNode->appendChild($pageNode);
		$pageNumNode    = $this->response->doc->createElement("numpage");
		$pageNumNode->appendChild($this->response->doc->createTextNode($this->numPerPage));
		$NavigationNode->appendChild($pageNumNode);

		$numPages = ceil($this->numMaps/$this->numPerPage);
		$midRange = ceil($this->range/2);
		/**echo " numMaps : ".$numMaps;
		 echo " numPages : ".$numPages;
		 echo " midRange : ".$midRange;
		 echo " page : ".$this->page;**/

		//--resolveRange--
		//start or less than $range pages of results
		if($this->page<($midRange) || $numPages<=$this->range){
			$start = 1;
			$end   = ($this->range<$numPages)?$this->range:$numPages;
			//end
		}else if($this->page>(($this->numMaps/$this->numPerPage)-($midRange))){
			$start = $numPages - $this->range;
			$end   = $numPages;
			//between
		}else{
			$start = $this->page-$midRange;
			$end   = $this->page+$midRange;
		}
		if($start<=0){
			$start += 1-$start;
			$end += 1-$start;
		}
		//echo "end : ".$end." start : ".$start;
		//disabled prev link
		if($this->page > $numPages){
			$this->page = $numPages;
		}

		if($this->page==1){
			$previousNode       = $this->response->doc->createElement("previous");
			$previousNode->appendChild($this->response->doc->createTextNode("disabled"));
			$NavigationNode->appendChild($previousNode);
		}
		//disabled next link
		if($this->page==$numPages){
			$nextNode       = $this->response->doc->createElement("next");
			$nextNode->appendChild($this->response->doc->createTextNode("disabled"));
			$NavigationNode->appendChild($nextNode);
		}

		//all number links to display
		$pagesLinksNode       = $this->response->doc->createElement("pageslinks");
		for($n=$start;$n<=$end;$n++){
			$pagelinkNode       = $this->response->doc->createElement("pagelink");
			$pagelinkNode->appendChild($this->response->doc->createTextNode($n));
			if($n == $this->page) $pagelinkNode->setAttribute("state","active");
			else $pagelinkNode->setAttribute("state","inactive");
			$pagesLinksNode->appendChild($pagelinkNode);
		}

		$NavigationNode->appendChild($pagesLinksNode);


	}

}

?>
